package com.pdf.pdfserver.service.write;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.pdf.pdfserver.config.OpenOfficeContext;
import com.pdf.pdfserver.util.PdfConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
 * @Author songfz
 * @Description pdf文件生成
 * @Date 2022/1/4
 */
@Service
public class PdfCreateService {

    private static Logger LOG = LoggerFactory.getLogger(PdfCreateService.class);


    private final OpenOfficeContext openOfficeContext;

    public PdfCreateService(OpenOfficeContext openOfficeContext) {
        this.openOfficeContext = openOfficeContext;
    }

    /**
     * 生成PDF
     * @param newFile pdf文件
     * @param context 文本内容
     * @return
     */
    public Boolean create(File newFile, String context) {



        // step 1: creation of a document-object
        Rectangle rect = new Rectangle(PageSize.A4);

        Document document = new Document(rect, 50, 50, 50, 50);


        try {


            // step 2: creation of the writer
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(newFile));


//            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
//            writer.setEncryption(DocWriter.getISOBytes("admin"),DocWriter.getISOBytes("admin"),PdfWriter.ALLOW_COPY, PdfWriter.STANDARD_ENCRYPTION_128);

            // step 3: we open the document
//            document.addTitle("标题");封面
//            document.addSubject("主题");
//            document.addKeywords("关键字");
//            document.addCreator("文档创建人");
//            document.addAuthor("文档内容作者");
            document.open();


            // step 4: we add a paragraph to the document
            document.add(new Paragraph(context, PdfConvertUtil.setChineseFont()));

            //不会显示空内容的页
            writer.setPageEmpty(true);
            return true;

        } catch (IOException | DocumentException de) {
            LOG.error(de.getMessage(), de);
            return false;
        } finally {

            // step 5: we close the document
            document.close();
        }
    }

    /**
     * PDF生成
     * @param inputFile 原文件
     * @param outputFile pdf文件
     * @return
     */
    public Boolean create(File inputFile,File outputFile){

        // connect to an OpenOffice.org instance running on port 8100
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                openOfficeContext.getHost(), openOfficeContext.getPort());
        try {
            connection.connect();
            if (inputFile.getName().endsWith(".pdf")) {
                Files.copy(inputFile.toPath(), outputFile.toPath(),REPLACE_EXISTING);
                return true;
            }

            // convert
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);

            return true;
        } catch (IOException e) {
            LOG.error(e.getMessage(),e);
        }finally {
            connection.disconnect();
        }
        return false;
    }


}
